package cmd

import (
	"encoding/json"
	"go-ws-server/businessService"
	"go-ws-server/model/common"
	"log"

	"github.com/gorilla/websocket"
)

// Cmd 解析 [Main]
func HandleMessage(userId string, msg []byte, conn *websocket.Conn) {

	var req common.WsCommonRequest

	// 1. 解析 JSON 消息为结构体
	if err := json.Unmarshal(msg, &req); err != nil {
		log.Println("❌ 消息解析失败:", err)
		return
	}
	log.Printf("✅ 收到用户 %s 的消息: %+v\n", userId, req)

	// 2. 按 Cmd 指令路由分发
	switch req.Cmd {

	//发送消息给指定用户
	case 100001:
		businessService.HandleSendToUser(userId, req, conn)

	// Ping Pong 心跳
	case 100002:
		businessService.HandlePing(userId, req, conn)

	case 100003:
		businessService.GetUserList(userId, req, conn)

	default:
		log.Println("⚠️ 未知指令:", req.Cmd)
	}
}
